package com.test.uiapp2.function.Calculator;

import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Calculator.CalculatorElement;
import com.test.uiapp2.tools.Result;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class CalculatorFunction extends TestBase {

    //计算机加法
    public  void CalculatorPlus(){
        try {
            startApp(BaseElement.JSQ);
            sleep(WAIT);
            if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists()){
                inspection();
                sleep(WAIT);
                Result randomnumber = randomnumbertwo();
                if(!randomnumber.getOnenumber().equals(randomnumber.getTwonumber())){
                    Log.i(TAG, "点击第一个值="+randomnumber.getOnenumber());
                    clickByText(randomnumber.getOnenumber());
                    sleep(STIME*3);
                    Log.i(TAG, "点击第二个值="+randomnumber.getTwonumber());
                    clickByText(randomnumber.getTwonumber());
                    plus();
                    sleep(STIME*3);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getNumber()) + Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));
                }else{
                    Log.i(TAG, "点击第一个值="+randomnumber.getOnenumber());
                    clickByText(randomnumber.getOnenumber());
                    sleep(STIME);
                    plus();
                    sleep(STIME);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getOnenumber()) + Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));

                }
                cls();
                sleep(WAIT);
                minimize();
            }else{
                verify("未进入计算器",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //计算器减法
    public  void CalculatorMinus(){
        try {
            startApp(BaseElement.JSQ);
            sleep(WAIT);
            if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists()){
                inspection();
                sleep(WAIT);
                Result randomnumber = randomnumbertwo();
                if(!randomnumber.getOnenumber().equals(randomnumber.getTwonumber())){
                    Log.i(TAG, "点击第一个值="+randomnumber.getOnenumber());
                    getTest(randomnumber.getOnenumber()).click();
                    sleep(STIME);
                    Log.i(TAG, "点击第一个值="+randomnumber.getTwonumber());
                    getTest(randomnumber.getTwonumber()).click();
                    minusign();
                    sleep(STIME);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getNumber()) - Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));
                }else{
                    Log.i(TAG, "点击第一个值="+randomnumber.getOnenumber());
                    getTest(randomnumber.getOnenumber()).click();
                    sleep(STIME);
//                    Log.i(TAG, "点击第一个值="+randomnumber.getTwonumber());
//                    getTest(randomnumber.getTwonumber()).click();
                    minusign();
                    sleep(STIME);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getOnenumber()) - Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));
                }
                cls();
                sleep(WAIT);
                minimize();
            }else{
                verify("未进入计算器",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //计算机乘法
    public void Calculatormultiplication(){
        try {
            startApp(BaseElement.JSQ);
            if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists()){
                //检查异常值的方法
                inspection();
                sleep(WAIT);
                Result randomnumber = randomnumbertwo();
                if(!randomnumber.getOnenumber().equals(randomnumber.getTwonumber())){
                    getTest(randomnumber.getOnenumber()).click();
                    sleep(STIME);
                    getTest(randomnumber.getTwonumber()).click();
                    multiplication();
                    sleep(STIME);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getNumber()) * Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));
                }else{
                    getTest(randomnumber.getOnenumber()).click();
                    sleep(STIME);
                    multiplication();
                    sleep(STIME);
                    String randomnumberone = randomnumberone();
                    getTest(randomnumberone).click();
                    eq();
                    int i = Integer.parseInt(randomnumber.getOnenumber()) * Integer.parseInt(randomnumberone);
                    Log.i(TAG, "预期计算结果="+i);
                    String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
                    Log.i(TAG, "实际计算结果="+text);
                    verify("计算结果不对",i==Integer.parseInt(text));
                }
                cls();
                sleep(WAIT);
                minimize();
            }else{
                verify("未进入计算器",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //计算机除法
    public void  Calculatordivision(){
        try {
            double c;
            startApp(BaseElement.JSQ);
            if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists()){
                //检查异常值的方法
                inspection();
                Result randomnumber = randomnumbertwo();
                getTest(randomnumber.getOnenumber()).click();
                sleep(WAIT);
                getTest(randomnumber.getTwonumber()).click();
                division();
                String randomnumberone = randomnumberone();
                getTest(randomnumberone).click();
                eq();
//                int i = Integer.parseInt(randomnumber.getNumber()) / Integer.parseInt(randomnumberone);
                c = (double)(Math.round(Integer.parseInt(randomnumber.getNumber())/Integer.parseInt(randomnumberone)));//这样为保持2位
                Log.i(TAG, "预期计算结果="+c);
//                String text = getId(BaseElement.CALCULATOR2,CalculatorElement.CRESULT).getText();
//                Log.i(TAG, "实际计v算结果="+text);
//                verify("计算结果不对",i==Integer.parseInt(text));
//                cls();
//                sleep(WAIT);
//                minimize();
            }else{
                verify("未进入计算器",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //计算器测试用例开始时，先检测是否存在其他值
    public  void  inspection(){
        try {
            sleep(WAIT);
            Log.i(TAG, "进入检测异常值的方法");
            String cresult="";
            String formula="";
            UiObject resultvalue = getId(BaseElement.CALCULATOR2, CalculatorElement.CRESULT);
            UiObject formulavalue = getId(BaseElement.CALCULATOR2, CalculatorElement.FORMULA);
            if(resultvalue.exists()){
                cresult = resultvalue.getText();
            }
            if(formulavalue.exists()){
                formula = formulavalue.getText();
            }
            int cresult_length = cresult.length();
            int formula_length1 = formula.length();
            if(cresult_length != 0 || formula_length1 != 0){
                swipe_right();
                startApp(BaseElement.JSQ);
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //随机生成一位数
    public String randomnumberone(){
        Random r = new Random();
        int i = r.nextInt(10);
        String number = String.valueOf(i);
        Log.i(TAG, "一位随机数为"+number);
        return number;
    }

    //随机生成两位数
    public Result randomnumbertwo(){
        Result r = new Result();
        int rnumber = (int)(Math.random()*90 + 10);
        Log.i(TAG, "两位随机数为"+rnumber);
        String number = String.valueOf(rnumber);
        String onenumber = number.substring(0, 1);
        String twonumber = number.substring(1);
        r.setOnenumber(onenumber);
        r.setTwonumber(twonumber);
        r.setNumber(number);
        return r;
    }

    public boolean result(int actualrs,int expectedrs){
        if(actualrs != expectedrs){
            return false;
        }else{
            return  true;
        }
    }

    //计算器-加号
    public void plus(){
        sleep(WAIT);
        if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_ADD).exists()){
            clickById(BaseElement.CALCULATOR2,CalculatorElement.OP_ADD);
        }else{
            verify("界面异常",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_ADD).exists());
        }
    }

    //计算器-乘号
    public void multiplication(){
        sleep(WAIT);
        if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL).exists()){
            clickById(BaseElement.CALCULATOR2,CalculatorElement.OP_MUL);
        }else{
            verify("界面异常",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_ADD).exists());
        }
    }

    //计算器-减号
    public void minusign(){
        sleep(WAIT);
        if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_SUB).exists()){
            clickById(BaseElement.CALCULATOR2,CalculatorElement.OP_SUB);
        }else{
            verify("界面异常",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_SUB).exists());
        }
    }

    //计算器-等于
    public void  eq(){
        sleep(WAIT);
        if(getId(BaseElement.CALCULATOR2,CalculatorElement.EQ).exists()){
            clickById(BaseElement.CALCULATOR2,CalculatorElement.EQ);
        }else{
            verify("界面异常",getId(BaseElement.CALCULATOR2,CalculatorElement.EQ).exists());
        }
    }

    //计算器-除法
    public void division(){
        sleep(WAIT);
        if(getId(BaseElement.CALCULATOR2,CalculatorElement.OP_DIV).exists()){
            clickById(BaseElement.CALCULATOR2,CalculatorElement.OP_DIV);
        }else{
            verify("界面异常",getId(BaseElement.CALCULATOR2,CalculatorElement.OP_DIV).exists());
        }
    }

    //清空数据
    public  void cls(){
        try {
            sleep(HWAIT);
            clickById(BaseElement.CALCULATOR2,CalculatorElement.OP_CLR);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
